import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

// 通知接口
interface Notification {
  id: string
  title: string
  message: string
  type: 'info' | 'success' | 'warning' | 'error' | 'system'
  read: boolean
  createdAt: string
  url?: string
}

export const useNotificationStore = defineStore('notification', () => {
  // 通知列表
  const notifications = ref<Notification[]>([])
  
  // 未读数量
  const unreadCount = computed(() => 
    notifications.value.filter(n => !n.read).length
  )
  
  // 获取通知列表
  const fetchNotifications = async () => {
    try {
      // 在实际项目中，这里应该调用真实的API
      // 暂时使用空数组，等待真实API集成
      notifications.value = []
    } catch (error) {
      // 静默处理错误
    }
  }
  
  // 标记为已读
  const markAsRead = (id: string) => {
    const notification = notifications.value.find(n => n.id === id)
    if (notification) {
      notification.read = true
    }
  }
  
  // 全部标记为已读
  const markAllAsRead = () => {
    notifications.value.forEach(notification => {
      notification.read = true
    })
  }
  
  // 添加新通知
  const addNotification = (notification: Omit<Notification, 'id' | 'createdAt'>) => {
    const newNotification: Notification = {
      ...notification,
      id: Date.now().toString(),
      createdAt: new Date().toISOString()
    }
    notifications.value.unshift(newNotification)
  }
  
  // 删除通知
  const removeNotification = (id: string) => {
    const index = notifications.value.findIndex(n => n.id === id)
    if (index > -1) {
      notifications.value.splice(index, 1)
    }
  }
  
  // 清空所有通知
  const clearAllNotifications = () => {
    notifications.value = []
  }
  
  // 获取未读通知数量
  const getUnreadCount = () => {
    return unreadCount.value
  }
  
  return {
    notifications,
    unreadCount,
    fetchNotifications,
    markAsRead,
    markAllAsRead,
    addNotification,
    removeNotification,
    clearAllNotifications,
    getUnreadCount
  }
})
